package com.zhangshitong.controller;

import java.util.List;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zhangshitong.common.JsonData;
import com.zhangshitong.dto.DeptLevelDto;
import com.zhangshitong.param.DeptParam;
import com.zhangshitong.service.SysDeptService;
import com.zhangshitong.service.SysTreeService;

@Controller
@RequestMapping("/sys/dept")
@Slf4j
public class SysDeptController {

	@Resource
	private SysDeptService sysDeptService;

	@Resource
	private SysTreeService sysTreeService;

	@RequestMapping("/save.json")
	@ResponseBody
	public JsonData saveDept(DeptParam param) {
		sysDeptService.save(param);
		return JsonData.success();
	}

	@RequestMapping("/dept.page")
	@ResponseBody
	public ModelAndView page() {
		return new ModelAndView("dept");
	}

	@RequestMapping("/tree.json")
	@ResponseBody
	public JsonData tree() {
		List<DeptLevelDto> dtoList = sysTreeService.deptTree();
		return JsonData.success(dtoList);
	}

	@RequestMapping("/update.json")
	@ResponseBody
	public JsonData updateDept(DeptParam param) {
		sysDeptService.update(param);
		return JsonData.success();
	}
	
	//有子部门不可以删除,当前部门下面有用户也不可以删除
	@RequestMapping("/delete.json")
    @ResponseBody
    public JsonData delete(@RequestParam("id") int id) {
        sysDeptService.delete(id);
        return JsonData.success();
    }
}
